#ifndef HTTP_H
#define HTTP_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include "winsock2.h"
#define close closesocket
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <iconv.h>
#include <wchar.h>
#endif

#define		FILENAME	1
#define		PORTNAME	2
#define		PATHNAME	3	
#define		SITENAME	4

#define		HTTP_UNKNOW	0
#define		HTTP_NORMAL	1
#define		HTTP_CHUNKED	2
#define		HTTP_CHUNKED_END	3
#define		HTTP_10_NO_SIZE	4

typedef void (*HTTP_CALLBACK)(char *buf, int size, void *arg);

#define BUFSIZE 2048
#define MAX_CHUNK_SIZE_STRING 20
struct http_task
{
	int state;
	int size;
	int remain;
	int chunked_extra;
	int cancel;
	int sd;
	char *url;
	char buf[BUFSIZE + MAX_CHUNK_SIZE_STRING];
	char *pos;
	int valid_size;
	HTTP_CALLBACK callback;
	void *arg;
};

struct http_task *create_http_task(char *url, HTTP_CALLBACK pCallBack, void *arg);
int start_http_download(struct http_task *task);
void cancel_http_download(struct http_task *task);
void free_http_task(struct http_task *task);

#endif
